#pragma once

#include<iostream>
#include<string>
#include<stdarg.h>
#include<ctime>
#include<sys/types.h>
#include<unistd.h>

// #define LOG_NOMAL "lognomal.txt"
// #define LOG_ERR "logerr.txt"


#define DEBUG 0
#define NOMAL 1
#define WARING 2
#define ERROR 3
#define FATAL 4

const char* to_levelstr(int level)
{
    switch(level)
    {
        case DEBUG: return "DEBUG";
        case NOMAL: return "NOMAL";
        case WARING: return "WARING";
        case ERROR: return "ERROR";
        case FATAL: return "FATAL";
        default: return nullptr;
    }
}
void logMessage(int level,const char* format,...)
{
    // [日志等级] [时间戳/时间] [pid] [message]
    // [WARING] [年月日分秒] [123] [创建socket失败]

#define NUM 1024
    char logprefix[NUM];
    snprintf(logprefix,sizeof(logprefix),"[%s][%ld][pid:%d]",to_levelstr(level),(long int)time(nullptr),getpid());// 可以用local time


    char logcontent[NUM];
    va_list arg;
    va_start(arg,format);
    
    vsnprintf(logcontent,sizeof(logcontent),format,arg);
    
    std::cout << logprefix << logcontent << std::endl;

    // FILE* log = fopen(LOG_NOMAL,"a");
    // FILE* err = fopen(LOG_ERR,"a");
    // if(log != nullptr && err != nullptr)
    // {
    //     FILE* cur = nullptr;
    //     if(level == DEBUG || level == NOMAL || level == WARING) cur = log;
    //     if(level == ERROR || level == FATAL) cur = err;
    //     if(cur) fprintf(cur,"%s%s\n",logprefix,logcontent);

    //     fclose(log);
    //     fclose(err);
    // }
}